<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no"/>
    <title>修改密码 - Nkuvr 后台管理系统</title>
    <meta name="author" content="yinqi">
    <link href="../../css/bootstrap.min.css" rel="stylesheet">
    <link href="../../css/materialdesignicons.min.css" rel="stylesheet">
    <link href="../../css/style.min.css" rel="stylesheet">
</head>

<body>
<div class="lyear-layout-web">
    <div class="lyear-layout-container">
        <!--左侧导航-->
        <aside th:replace="fragment/_fragment::navbar"></aside>


        <!--头部信息-->
        <header th:replace="fragment/_fragment::sidebar"></header>


        <!--页面主要内容-->
        <main class="lyear-layout-content">

            <div class="container-fluid">

                <div class="row">
                    <div class="col-lg-12">
                        <div class="card">
                            <div class="card-body">

                                <form action="#!" method="post" class="row">
                                    <input type="hidden" id="id" name="id" th:value="${userInfo.id}">
                                    <div class="form-group col-md-12">
                                        <label for="password">原来密码</label>
                                        <input type="password" class="form-control" id="password" name="password"/>
                                    </div>

                                    <div class="form-group col-md-12">
                                        <label for="newPassword">新的密码</label>
                                        <input type="password" class="form-control" id="newPassword"
                                               name="newPassword"/>
                                    </div>

                                    <div class="form-group col-md-12">
                                        <label for="confirmPassword">确认密码</label>
                                        <input type="password" class="form-control" id="confirmPassword"
                                               name="confirmPassword"/>
                                    </div>

                                    <div class="form-group col-md-12">
                                        <a class="btn btn-primary ajax-post" target-form="add-form"
                                           href="javascript:void(0);" onclick="return doChangePassword();">确 定</a>
                                        <button type="button" class="btn btn-default"
                                                onclick="javascript:window.history.back(-1);return false;">返 回
                                        </button>
                                    </div>
                                </form>

                            </div>
                        </div>
                    </div>

                </div>

            </div>

        </main>

    </div>
</div>

<script type="text/javascript" src="../../js/jquery.min.js"></script>
<script type="text/javascript" src="../../js/bootstrap.min.js"></script>
<script type="text/javascript" src="../../js/perfect-scrollbar.min.js"></script>
<script type="text/javascript" src="../../js/main.min.js"></script>
<script src="../../layer/layer.js"></script>
<script type="text/javascript">
    function doChangePassword() {
        var id = $('#id').val();
        var password = $('#password').val();
        var newPassword = $('#newPassword').val();
        var confirmPassword = $('#confirmPassword').val();

        var loadingIndex = null;

        if (password === "") {
            layer.msg("原来密码不能为空，请输入!", {time: 1000, icon: 5, shift: 6});
            return;
        }

        if (newPassword === "") {
            layer.msg("新的密码不能为空，请输入!", {time: 1000, icon: 5, shift: 6});
            return;
        }

        if (confirmPassword === "") {
            layer.msg("确认密码不能为空，请输入!", {time: 1000, icon: 5, shift: 6});
            return;
        }

        if (newPassword !== confirmPassword) {
            layer.msg("输入密码不一致，请检查!", {time: 1000, icon: 5, shift: 6});
            return;
        }


        layer.confirm("确定修改密码吗？", {btn: ['确认', '取消']}, function () {
            $.ajax({
                type: "post",
                url: "/user/doChangePassword",
                async: false,
                dataType: "json",
                data: {
                    "id": id,
                    "password": newPassword
                },
                beforeSend: function () {
                    loadingIndex = layer.msg("处理中", {icon: 16});
                },
                success: function (result) {
                    layer.close(loadingIndex);
                    if (result.success) {
                        layer.msg("密码修改成功", {time: 1000, icon: 6});
                        window.location.href = "/user/profile/" + id;
                    } else {
                        layer.msg("密码修改失败，请重新操作", {time: 1000, icon: 5, shift: 6});
                    }
                }
            });
            return false;
        });
    }
</script>
</body>
</html>